package com.ly.mpayment.rest.up.napi;

import com.alibaba.fastjson.JSONObject;
import com.ly.mpayment.core.secuity.MD5;
import com.ly.mpayment.core.util.*;
import com.ly.mpayment.persist.entity.UpOrder;
import com.ly.mpayment.persist.entity.UpShopInfo;
import com.ly.mpayment.persist.service.downrechargeflow.IDownRechargeFlowService;
import com.ly.mpayment.persist.service.downshopinfo.IDownShopInfoService;
import com.ly.mpayment.persist.service.uporder.IUporderService;
import com.ly.mpayment.persist.service.upshopinfo.IUpShopInfoService;
import com.ly.mpayment.rest.recharge.RechargeRest;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.RequestBody;

import javax.ws.rs.POST;
import javax.ws.rs.Path;

@Component
@Path("napikeji")
public class NaPiRest {

    Logger logger = Logger.getLogger(NaPiRest.class);

    @Autowired
    IUporderService upOrderService;

    @Autowired
    private ThreadPoolTaskExecutor taskExecutor;

    @Autowired
    IDownRechargeFlowService flowService;

    @Autowired
    IDownShopInfoService shopService;

    @Autowired
    IUpShopInfoService upShopInfoService;

    public void queryOrder(UpOrder order, UpShopInfo shop) {
        JSONObject data = new JSONObject();
        data.put("appid",shop.getAppId());
        data.put("order_sn",order.getUporderuuid());
        String result = HttpClient.httpGetUrl(shop.getOrderStatusAddress().concat("?")+StringSort.jsonToString(data));
        logger.info("resultnapi:"+result);
        if(result != null){
            JSONObject map = JSONObject.parseObject(result);
            if(map != null){
                //订单状态 // 0失败1成功 2处理中
                if(map.getString("error_code").equals("0") && map.getString("order_status").equals("2")){
                    order.setRemark( map.containsKey("official_sns") ? map.getJSONArray("official_sns").getJSONObject(0).getString("official_sn") : map.getString("msg"));
                    RechargeRest.successUpOrder.add(order);
                } else if(map.getString("error_code").equals("0") && map.getString("order_status").equals("3")){
                    order.setRemark(map.getString("msg"));
                    RechargeRest.failtUpOrder.add(order);
                }else {
                    upOrderService.updateUpOrderArtificial(order, "",map.getString("msg"));
                }
            }else{
                upOrderService.updateUpOrderArtificial(order, "","查询失败:"+result);
            }
        }else{
            //不处理 走查询 确认
            upOrderService.updateUpOrderArtificial(order, "","查询失败:"+result);
            logger.info("查询失败"+order.getUpphonenum());
        }
    }
    public void commitOrder(UpOrder order, UpShopInfo shop) {
        JSONObject data = new JSONObject();
        data.put("appid",shop.getAppId());
        data.put("order_sn",order.getUporderuuid());
        data.put("recharge_account",order.getUpphonenum());
        data.put("recharge_amount",order.getUpShopPro().getUpfacevalue());
        data.put("timestamp", System.currentTimeMillis() / 1000);
        final String sign = StringSort.doSign(data);
        //md5(order_sn+recharge_account+recharge_amount+timestamp+appkey)结果取32位小写
        data.put("sign", MD5.md5( order.getUporderuuid()+order.getUpphonenum()+order.getUpShopPro().getUpfacevalue()+data.getString("timestamp") + shop.getUpkey()));
        String result = HttpClient.httpGetUrl(shop.getOrderCommitAddress().concat("?")+StringSort.jsonToString(data));
        logger.info("commitOrder_result:"+result);
        if(result != null){
            JSONObject map = JSONObject.parseObject(result);
            if(map != null){
                if(map.getString("error_code").equals("0")){
                    upOrderService.updateUpOrderMsg(order, map.getString("order_sn"), map.getString("msg"));
                }else if(
                        map.getString("error_code").equals("1000") ||
                        map.getString("error_code").equals("1001") ||
                        map.getString("error_code").equals("1002") ||
                        map.getString("error_code").equals("1003") ||
                        map.getString("error_code").equals("1004") ||
                        map.getString("error_code").equals("2000") ||
                        map.getString("error_code").equals("2001") ||
                        map.getString("error_code").equals("2003") ||
                        map.getString("error_code").equals("2004") ||
                        map.getString("error_code").equals("2005") ||
                        map.getString("error_code").equals("2006") ||
                        map.getString("error_code").equals("2007") ||
                        map.getString("error_code").equals("2008") ||
                        map.getString("error_code").equals("2009")){
                    order.setRemark(map.getString("msg"));
                    RechargeRest.failtUpOrder.add(order);
                }else{
                    upOrderService.updateUpOrderMsg(order, "",map.getString("msg"));
                }
            }else{
                upOrderService.updateUpOrderMsg(order, "","转换失败:"+result);
            }
        }else{
            //不处理 走查询 确认
            upOrderService.updateUpOrderMsg(order, "","提单失败:"+result);
            logger.info("提单失败"+order.getUpphonenum());
        }
    }

    @Path("callBack")
    @POST
    public String callBack(@RequestBody String body){
        logger.info("napicallBack:"+body);
        if(!StringUtils.isBlank(body)){
            JSONObject data = JSONObject.parseObject(body);
            UpOrder order = upOrderService.findById(data.getString("order_sn"));
            if(order != null ){
                //订单状态 // 0失败1成功 2处理中
                if (order != null && data.getString("order_status").equals("2") && order.getStatus() == Config.status[0]) {
                    order.setRemark(data.getString("official_sn") == null ? "" : data.getString("official_sn"));
                    RechargeRest.successUpOrder.add(order);
                    return "ok";
                }else if(order != null && order.getStatus() == Config.status[0] && data.getString("order_status").equals("3") ){
                    order.setRemark(data.getString("order_status"));
                    RechargeRest.failtUpOrder.add(order);
                    return "ok";
                }else{
                    logger.info("napicallBack_not_found_order:" + body );
                }
            }else {
                logger.info("未找到订单:"+body);
            }
        }
        return "failt";
    }
}
